﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Vxl;
using Vxl.Drawables;
using SlimDX;
using Vxl.Cameras;
using Vxl.Utils;

namespace VxlEditor
{
    public partial class ObjectCreator : UserControl
    {
        VxlObject currentVxlObject = null;

        public ObjectCreator()
        {
            InitializeComponent();
        }

        public void CleanUp(){
            this.vxlEngine.CleanUp();
        }

        public void InitializeVxlComponent(){
            this.vxlEngine.InitializeVxlComponents();
            VxlEngineSetupHelper.vxlEngine.SetCamera(new ModelCamera(new Vector3(1, 0, 0), Vector3.Zero));
        }

        public void SerializeVxlObject()
        {
            DialogResult result = this.saveVxlObjDialog.ShowDialog();
            if (result == DialogResult.Cancel)
                return;
            
            VxlSerializer.SaveVxlObject(this.saveVxlObjDialog.FileName, ref currentVxlObject);
        }

        public void DeSerializeVxlObject()
        {

            DialogResult result = this.loadVxlObjDialog.ShowDialog();

            if (result == DialogResult.Cancel)
                return;

            this.vxlEngine.Stop();
            if(currentVxlObject != null)
                VxlEngineSetupHelper.vxlEngine.RemoveVxlObject(ref currentVxlObject);

            currentVxlObject = VxlSerializer.LoadVxlObject(this.loadVxlObjDialog.FileName);
            currentVxlObject.InitializeFromLoad();
            VxlEngineSetupHelper.vxlEngine.AddVxlObject(ref currentVxlObject);
            this.vxlEngine.Start();
        }

        public void New()
        {

            vxlEngine.Stop();
            // ask want to save?
            // if so show save dialog
            if (VxlEngineSetupHelper.vxlEngine.VxlObjectCount != 0)
            {
                DialogResult saveOrNotToSave = MessageBox.Show("Do you want to save the existing vxl?", "New", MessageBoxButtons.YesNoCancel);
                if(saveOrNotToSave  == DialogResult.Yes)
                {
                    SerializeVxlObject();
                }
                else if(saveOrNotToSave == DialogResult.Cancel){

                    vxlEngine.Start();
                    return;
                }
            }

            VxlEngineSetupHelper.vxlEngine.RemoveVxlObject(ref currentVxlObject);

            currentVxlObject = new VxlObject();
            currentVxlObject.Initialize();
            VxlEngineSetupHelper.vxlEngine.AddVxlObject(ref currentVxlObject);
            vxlEngine.Start();
            // create new object in the vxl engine
        }

        private void vxlColorPanel_Click(object sender, EventArgs e){
            /*DialogResult result = this.vxlColorDialog.ShowDialog();
            if (result == DialogResult.OK)
                this.vxlColorPanel.BackColor = this.vxlColorDialog.Color;
            else
                MessageBox.Show("Could not change the color");
             * */
        }

        private void addVxlButton_Click(object sender, EventArgs e)
        {
            vxlEngine.Stop();
            if(currentVxlObject == null){             
                currentVxlObject = new VxlObject();
                VxlEngineSetupHelper.vxlEngine.AddVxlObject(ref currentVxlObject);
            }
            currentVxlObject.AddVxl(
                    new Vector3(
                        (int)this.xPosValue.Value,
                        (int)this.yPosValue.Value,
                        (int)this.zPosValue.Value),
                    new Vector4(
                        ((float)this.vxlColorPanel.BackColor.R / (float)255),
                        ((float)this.vxlColorPanel.BackColor.G / (float)255),
                        ((float)this.vxlColorPanel.BackColor.B / (float)255),
                        ((float)this.vxlColorPanel.BackColor.A / (float)255)));
            currentVxlObject.ResetVertexBuffer();

            this.vxlList.Items.Add("( " + (int)this.xPosValue.Value + " , " + (int)this.yPosValue.Value + " , " + (int)this.zPosValue.Value + " )");            
            this.vxlList.Update();
            vxlEngine.Start();
        }

        private bool shrunken = true;
        private bool mouseOverColorPicker = false;

        private void vxlColorPanel_Enter(object sender, EventArgs e)
        {
            mouseOverColorPicker = true;
            GrowColorPicker();
        }

        private void vxlColorPanel_Leave(object sender, EventArgs e)
        {
            //mouseOverColorPanel = false;
            //ShrinkColorPicker();
        }

        private void vxlColorChooser1_MouseEnter(object sender, EventArgs e)
        {
            mouseOverColorPicker = true;
            //GrowColorPicker();
        }

        private void vxlColorChooser1_MouseLeave(object sender, EventArgs e)
        {
            mouseOverColorPicker = false;
            ShrinkColorPicker();
        }
        
        private void ShrinkColorPicker()
        {
            // Shrink
            if (!shrunken && !mouseOverColorPicker/* && !mouseOverColorPanel*/)
            {
                this.vxlColorChooser1.Enabled = false;
                this.vxlColorChooser1.Visible = false;
                
                this.vxlColorChooser1.Invalidate();
                shrunken = true;
            }
        }

        private void GrowColorPicker()
        {
            // Grow
            if (shrunken)
            {
                this.vxlColorChooser1.Enabled = true;
                this.vxlColorChooser1.Visible = true;
                this.vxlColorChooser1.Focus();
                this.vxlColorChooser1.Select();
                this.vxlColorChooser1.Invalidate();
                shrunken = false;
            }
        }

        private void vxlColorChooser1_MouseHover(object sender, EventArgs e)
        {
            mouseOverColorPicker = true;
            GrowColorPicker();
        }
    }
}
